home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / flex / dist / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-19  |  16.2 KB  |  662 lines

  1. /* flex - tool to generate fast lexical analyzers
  2.  *
  3.  *
  4.  * Copyright (c) 1989 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant to
  11.  * contract no. DE-AC03-76SF00098 between the United States Department of
  12.  * Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted
  15.  * provided that the above copyright notice and this paragraph are
  16.  * duplicated in all such forms and that any documentation,
  17.  * advertising materials, and other materials related to such
  18.  * distribution and use acknowledge that the software was developed
  19.  * by the University of California, Berkeley.  The name of the
  20.  * University may not be used to endorse or promote products derived
  21.  * from this software without specific prior written permission.
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  23.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25.  *
  26.  */
  27.  
  28. #ifndef lint
  29.  
  30. static char copyright[] =
  31.     "@(#) Copyright (c) 1989 The Regents of the University of California.\n";
  32. static char CR_continuation[] = "@(#) All rights reserved.\n";
  33.  
  34. static char rcsid[] =
  35.     "@(#) $Header: main.c,v 2.2 89/06/20 16:36:26 vern Exp $ (LBL)";
  36.  
  37. #endif
  38.  
  39.  
  40. #include "flexdef.h"
  41.  
  42. static char flex_version[] = "2.1 (beta)";
  43.  
  44.  
  45. /* these globals are all defined and commented in flexdef.h */
  46. int printstats, syntaxerror, eofseen, ddebug, trace, spprdflt;
  47. int interactive, caseins, useecs, fulltbl, usemecs;
  48. int fullspd, gen_line_dirs, performance_report, backtrack_report;
  49. int yymore_used, reject, real_reject, continued_action;
  50. int yymore_really_used, reject_really_used;
  51. int datapos, dataline, linenum;
  52. FILE *skelfile = NULL;
  53. char *infilename = NULL;
  54. int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  55. int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  56. int current_mns, num_rules, current_max_rules, lastnfa;
  57. int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
  58. int *accptnum, *assoc_rule, *state_type, *rule_type, *rule_linenum;
  59. int current_state_type;
  60. int variable_trailing_context_rules;
  61. int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  62. int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  63. int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, tecfwd[CSIZE + 1];
  64. int tecbck[CSIZE + 1];
  65. int lastsc, current_max_scs, *scset, *scbol, *scxclu, *sceof, *actvsc;
  66. char **scname;
  67. int current_max_dfa_size, current_max_xpairs;
  68. int current_max_template_xpairs, current_max_dfas;
  69. int lastdfa, *nxt, *chk, *tnxt;
  70. int *base, *def, tblend, firstfree, **dss, *dfasiz;
  71. union dfaacc_union *dfaacc;
  72. int *accsiz, *dhash, numas;
  73. int numsnpairs, jambase, jamstate;
  74. int lastccl, current_maxccls, *cclmap, *ccllen, *cclng, cclreuse;
  75. int current_max_ccl_tbl_size;
  76. char *ccltbl;
  77. char *starttime, *endtime, nmstr[MAXLINE];
  78. int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  79. int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  80. int num_backtracking, bol_needed;
  81. FILE *temp_action_file;
  82. FILE *backtrack_file;
  83. int end_of_buffer_state;
  84. #ifndef SHORT_FILE_NAMES
  85. char action_file_name[] = "/tmp/flexXXXXXX";
  86. #else
  87. char action_file_name[] = "flexXXXXXX.tmp";
  88. #endif
  89.  
  90. #ifndef SHORT_FILE_NAMES
  91. static char outfile[] = "lex.yy.c";
  92. #else
  93. static char outfile[] = "lexyy.c";
  94. #endif
  95. static int outfile_created = 0;
  96.  
  97.  
  98. /* flex - main program
  99.  *
  100.  * synopsis (from the shell)
  101.  *    flex [-v] [file ...]
  102.  */
  103.  
  104. main( argc, argv )
  105. int argc;
  106. char **argv;
  107.  
  108.     {
  109.     flexinit( argc, argv );
  110.  
  111.     readin();
  112.  
  113.     if ( syntaxerror )
  114.     flexend( 1 );
  115.  
  116.     if ( yymore_really_used == REALLY_USED )
  117.     yymore_used = true;
  118.     else if ( yymore_really_used == REALLY_NOT_USED )
  119.     yymore_used = false;
  120.  
  121.     if ( reject_really_used == REALLY_USED )
  122.     reject = true;
  123.     else if ( reject_really_used == REALLY_NOT_USED )
  124.     reject = false;
  125.  
  126.     if ( performance_report )
  127.     {
  128.     if ( yymore_used )
  129.         fprintf( stderr,
  130.              "yymore() entails a minor performance penalty\n" );
  131.  
  132.     if ( interactive )
  133.         fprintf( stderr,
  134.          "-I (interactive) entails a minor performance penalty\n" );
  135.  
  136.     if ( reject )
  137.         fprintf( stderr,
  138.              "REJECT entails a large performance penalty\n" );
  139.  
  140.     if ( variable_trailing_context_rules )
  141.         fprintf( stderr,
  142. "Variable trailing context rules entail a large performance penalty\n" );
  143.     }
  144.  
  145.     if ( reject )
  146.     real_reject = true;
  147.  
  148.     if ( variable_trailing_context_rules )
  149.     reject = true;
  150.  
  151.     if ( (fulltbl || fullspd) && reject )
  152.     {
  153.     if ( real_reject )
  154.         flexerror( "REJECT cannot be used with -f or -F" );
  155.     else
  156.         flexerror(
  157.     "variable trailing context rules cannot be used with -f or -F" );
  158.     }
  159.  
  160.     /* convert the ndfa to a dfa */
  161.     ntod();
  162.  
  163.     /* generate the C state transition tables from the DFA */
  164.     make_tables();
  165.  
  166.     /* note, flexend does not return.  It exits with its argument as status. */
  167.  
  168.     flexend( 0 );
  169.  
  170.     /*NOTREACHED*/
  171.     }
  172.  
  173.  
  174. /* flexend - terminate flex
  175.  *
  176.  * synopsis
  177.  *    int status;
  178.  *    flexend( status );
  179.  *
  180.  *    status is exit status.
  181.  *
  182.  * note
  183.  *    This routine does not return.
  184.  */
  185.  
  186. flexend( status )
  187. int status;
  188.  
  189.     {
  190.     int tblsiz;
  191.     char *flex_gettime();
  192.  
  193.     if ( skelfile != NULL )
  194.     (void) fclose( skelfile );
  195.  
  196.     if ( temp_action_file )
  197.     {
  198.     (void) fclose( temp_action_file );
  199.     (void) unlink( action_file_name );
  200.     }
  201.  
  202.     if ( status != 0 && outfile_created )
  203.     {
  204.     (void) fclose( stdout );
  205.     (void) unlink( outfile );
  206.     }
  207.  
  208.     if ( backtrack_report )
  209.     {
  210.     if ( num_backtracking == 0 )
  211.         fprintf( backtrack_file, "No backtracking.\n" );
  212.     else if ( fullspd || fulltbl )
  213.         fprintf( backtrack_file,
  214.              "%d backtracking (non-accepting) states.\n",
  215.              num_backtracking );
  216.     else
  217.         fprintf( backtrack_file, "Compressed tables always backtrack.\n" );
  218.  
  219.     (void) fclose( backtrack_file );
  220.     }
  221.  
  222.     if ( printstats )
  223.     {
  224.     endtime = flex_gettime();
  225.  
  226.     fprintf( stderr, "flex version %s usage statistics:\n", flex_version );
  227.     fprintf( stderr, "  started at %s, finished at %s\n",
  228.          starttime, endtime );
  229.  
  230.     fprintf( stderr, "  %d/%d NFA states\n", lastnfa, current_mns );
  231.     fprintf( stderr, "  %d/%d DFA states (%d words)\n", lastdfa,
  232.              current_max_dfas, totnst );
  233.     fprintf( stderr, "  %d rules\n", num_rules - 1 /* - 1 for def. rule */ );
  234.  
  235.     if ( num_backtracking == 0 )
  236.         fprintf( stderr, "  No backtracking\n" );
  237.     else if ( fullspd || fulltbl )
  238.         fprintf( stderr, "  %d backtracking (non-accepting) states\n",
  239.              num_backtracking );
  240.     else
  241.         fprintf( stderr, "  compressed tables always backtrack\n" );
  242.  
  243.     if ( bol_needed )
  244.         fprintf( stderr, "  Beginning-of-line patterns used\n" );
  245.  
  246.     fprintf( stderr, "  %d/%d start conditions\n", lastsc,
  247.              current_max_scs );
  248.     fprintf( stderr, "  %d epsilon states, %d double epsilon states\n",
  249.          numeps, eps2 );
  250.  
  251.     if ( lastccl == 0 )
  252.         fprintf( stderr, "  no character classes\n" );
  253.     else
  254.         fprintf( stderr,
  255.     "  %d/%d character classes needed %d/%d words of storage, %d reused\n",
  256.              lastccl, current_maxccls,
  257.              cclmap[lastccl] + ccllen[lastccl],
  258.              current_max_ccl_tbl_size, cclreuse );
  259.  
  260.     fprintf( stderr, "  %d state/nextstate pairs created\n", numsnpairs );
  261.     fprintf( stderr, "  %d/%d unique/duplicate transitions\n",
  262.          numuniq, numdup );
  263.  
  264.     if ( fulltbl )
  265.         {
  266.         tblsiz = lastdfa * numecs;
  267.         fprintf( stderr, "  %d table entries\n", tblsiz );
  268.         }
  269.  
  270.     else
  271.         {
  272.         tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
  273.  
  274.         fprintf( stderr, "  %d/%d base-def entries created\n",
  275.              lastdfa + numtemps, current_max_dfas );
  276.         fprintf( stderr, "  %d/%d (peak %d) nxt-chk entries created\n",
  277.              tblend, current_max_xpairs, peakpairs );
  278.         fprintf( stderr,
  279.              "  %d/%d (peak %d) template nxt-chk entries created\n",
  280.              numtemps * nummecs, current_max_template_xpairs,
  281.              numtemps * numecs );
  282.         fprintf( stderr, "  %d empty table entries\n", nummt );
  283.         fprintf( stderr, "  %d protos created\n", numprots );
  284.         fprintf( stderr, "  %d templates created, %d uses\n",
  285.              numtemps, tmpuses );
  286.         }
  287.  
  288.     if ( useecs )
  289.         {
  290.         tblsiz = tblsiz + CSIZE;
  291.         fprintf( stderr, "  %d/%d equivalence classes created\n",
  292.              numecs, CSIZE );
  293.         }
  294.  
  295.     if ( usemecs )
  296.         {
  297.         tblsiz = tblsiz + numecs;
  298.         fprintf( stderr, "  %d/%d meta-equivalence classes created\n",
  299.              nummecs, CSIZE );
  300.         }
  301.  
  302.     fprintf( stderr, "  %d (%d saved) hash collisions, %d DFAs equal\n",
  303.          hshcol, hshsave, dfaeql );
  304.     fprintf( stderr, "  %d sets of reallocations needed\n", num_reallocs );
  305.     fprintf( stderr, "  %d total table entries needed\n", tblsiz );
  306.     }
  307.  
  308. #ifndef VMS
  309.     exit( status );
  310. #else
  311.     exit( status + 1 );
  312. #endif
  313.     }
  314.  
  315.  
  316. /* flexinit - initialize flex
  317.  *
  318.  * synopsis
  319.  *    int argc;
  320.  *    char **argv;
  321.  *    flexinit( argc, argv );
  322.  */
  323.  
  324. flexinit( argc, argv )
  325. int argc;
  326. char **argv;
  327.  
  328.     {
  329.     int i, sawcmpflag, use_stdout;
  330.     char *arg, *skelname = NULL, *flex_gettime(), clower(), *mktemp();
  331.  
  332.     printstats = syntaxerror = trace = spprdflt = interactive = caseins = false;
  333.     backtrack_report = performance_report = ddebug = fulltbl = fullspd = false;
  334.     yymore_used = continued_action = reject = false;
  335.     yymore_really_used = reject_really_used = false;
  336.     gen_line_dirs = usemecs = useecs = true;
  337.  
  338.     sawcmpflag = false;
  339.     use_stdout = false;
  340.  
  341.     /* read flags */
  342.     for ( --argc, ++argv; argc ; --argc, ++argv )
  343.     {
  344.     if ( argv[0][0] != '-' || argv[0][1] == '\0' )
  345.         break;
  346.  
  347.     arg = argv[0];
  348.  
  349.     for ( i = 1; arg[i] != '\0'; ++i )
  350.         switch ( arg[i] )
  351.         {
  352.         case 'b':
  353.             backtrack_report = true;
  354.             break;
  355.  
  356.         case 'c':
  357.             if ( i != 1 )
  358.             flexerror( "-c flag must be given separately" );
  359.  
  360.             if ( ! sawcmpflag )
  361.             {
  362.             useecs = false;
  363.             usemecs = false;
  364.             fulltbl = false;
  365.             sawcmpflag = true;
  366.             }
  367.  
  368.             for ( ++i; arg[i] != '\0'; ++i )
  369.             switch ( clower( arg[i] ) )
  370.                 {
  371.                 case 'e':
  372.                 useecs = true;
  373.                 break;
  374.  
  375.                 case 'F':
  376.                 fullspd = true;
  377.                 break;
  378.  
  379.                 case 'f':
  380.                 fulltbl = true;
  381.                 break;
  382.  
  383.                 case 'm':
  384.                 usemecs = true;
  385.                 break;
  386.  
  387.                 default:
  388.                 lerrif( "unknown -c option %c",
  389.                     (int) arg[i] );
  390.                 break;
  391.                 }
  392.             
  393.             goto get_next_arg;
  394.  
  395.         case 'd':
  396.             ddebug = true;
  397.             break;
  398.  
  399.         case 'f':
  400.             useecs = usemecs = false;
  401.             fulltbl = true;
  402.             break;
  403.  
  404.         case 'F':
  405.             useecs = usemecs = false;
  406.             fullspd = true;
  407.             break;
  408.  
  409.         case 'I':
  410.             interactive = true;
  411.             break;
  412.  
  413.         case 'i':
  414.             caseins = true;
  415.             break;
  416.  
  417.         case 'L':
  418.             gen_line_dirs = false;
  419.             break;
  420.  
  421.         case 'p':
  422.             performance_report = true;
  423.             break;
  424.  
  425.         case 'S':
  426.             if ( i != 1 )
  427.             flexerror( "-S flag must be given separately" );
  428.  
  429.             skelname = arg + i + 1;
  430.             goto get_next_arg;
  431.  
  432.         case 's':
  433.             spprdflt = true;
  434.             break;
  435.  
  436.         case 't':
  437.             use_stdout = true;
  438.             break;
  439.  
  440.         case 'T':
  441.             trace = true;
  442.             break;
  443.  
  444.         case 'v':
  445.             printstats = true;
  446.             break;
  447.  
  448.         default:
  449.             lerrif( "unknown flag %c", (int) arg[i] );
  450.             break;
  451.         }
  452.  
  453. get_next_arg: /* used by -c and -S flags in lieu of a "continue 2" control */
  454.     ;
  455.     }
  456.  
  457.     if ( (fulltbl || fullspd) && usemecs )
  458.     flexerror( "full table and -cm don't make sense together" );
  459.  
  460.     if ( (fulltbl || fullspd) && interactive )
  461.     flexerror( "full table and -I are (currently) incompatible" );
  462.  
  463.     if ( fulltbl && fullspd )
  464.     flexerror( "full table and -F are mutually exclusive" );
  465.  
  466.     if ( ! skelname )
  467.     {
  468.     static char skeleton_name_storage[400];
  469.  
  470.     skelname = skeleton_name_storage;
  471.     (void) strcpy( skelname, DEFAULT_SKELETON_FILE );
  472.     }
  473.  
  474.     if ( ! use_stdout )
  475.     {
  476.     FILE *prev_stdout = freopen( outfile, "w", stdout );
  477.  
  478.     if ( prev_stdout == NULL )
  479.         flexerror( "could not create lex.yy.c" );
  480.  
  481.     outfile_created = 1;
  482.     }
  483.  
  484.     if ( argc )
  485.     {
  486.     if ( argc > 1 )
  487.         flexerror( "extraneous argument(s) given" );
  488.  
  489.     yyin = fopen( infilename = argv[0], "r" );
  490.  
  491.     if ( yyin == NULL )
  492.         lerrsf( "can't open %s", argv[0] );
  493.     }
  494.  
  495.     else
  496.     yyin = stdin;
  497.  
  498.     if ( backtrack_report )
  499.     {
  500. #ifndef SHORT_FILE_NAMES
  501.     backtrack_file = fopen( "lex.backtrack", "w" );
  502. #else
  503.     backtrack_file = fopen( "lex.bck", "w" );
  504. #endif
  505.  
  506.     if ( backtrack_file == NULL )
  507.         flexerror( "could not create lex.backtrack" );
  508.     }
  509.  
  510.     else
  511.     backtrack_file = NULL;
  512.  
  513.  
  514.     lastccl = 0;
  515.     lastsc = 0;
  516.  
  517.     /* initialize the statistics */
  518.     starttime = flex_gettime();
  519.  
  520.     if ( (skelfile = fopen( skelname, "r" )) == NULL )
  521.     lerrsf( "can't open skeleton file %s", skelname );
  522.  
  523.     (void) mktemp( action_file_name );
  524.  
  525.     if ( (temp_action_file = fopen( action_file_name, "w" )) == NULL )
  526.     lerrsf( "can't open temporary action file %s", action_file_name );
  527.  
  528.     lastdfa = lastnfa = num_rules = numas = numsnpairs = tmpuses = 0;
  529.     numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 0;
  530.     numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
  531.     num_backtracking = onesp = numprots = 0;
  532.     variable_trailing_context_rules = bol_needed = false;
  533.  
  534.     linenum = sectnum = 1;
  535.     firstprot = NIL;
  536.  
  537.     /* used in mkprot() so that the first proto goes in slot 1
  538.      * of the proto queue
  539.      */
  540.     lastprot = 1;
  541.  
  542.     if ( useecs )
  543.     {
  544.     /* set up doubly-linked equivalence classes */
  545.     ecgroup[1] = NIL;
  546.  
  547.     for ( i = 2; i <= CSIZE; ++i )
  548.         {
  549.         ecgroup[i] = i - 1;
  550.         nextecm[i - 1] = i;
  551.         }
  552.  
  553.     nextecm[CSIZE] = NIL;
  554.     }
  555.  
  556.     else
  557.     { /* put everything in its own equivalence class */
  558.     for ( i = 1; i <= CSIZE; ++i )
  559.         {
  560.         ecgroup[i] = i;
  561.         nextecm[i] = BAD_SUBSCRIPT;    /* to catch errors */
  562.         }
  563.     }
  564.  
  565.     set_up_initial_allocations();
  566.     }
  567.  
  568.  
  569. /* readin - read in the rules section of the input file(s)
  570.  *
  571.  * synopsis
  572.  *    readin();
  573.  */
  574.  
  575. readin()
  576.  
  577.     {
  578.     if ( ddebug )
  579.     puts( "#define FLEX_DEBUG" );
  580.  
  581.     if ( fulltbl )
  582.     puts( "#define FLEX_FULL_TABLE" );
  583.     else if ( fullspd )
  584.     puts( "#define FLEX_FAST_COMPRESSED" );
  585.     else
  586.     puts( "#define FLEX_COMPRESSED" );
  587.  
  588.     skelout();
  589.  
  590.     line_directive_out( stdout );
  591.  
  592.     if ( yyparse() )
  593.     lerrif( "fatal parse error at line %d", linenum );
  594.  
  595.     if ( useecs )
  596.     {
  597.     numecs = cre8ecs( nextecm, ecgroup, CSIZE );
  598.     ccl2ecl();
  599.     }
  600.  
  601.     else
  602.     numecs = CSIZE;
  603.  
  604.     }
  605.  
  606.  
  607.  
  608. /* set_up_initial_allocations - allocate memory for internal tables */
  609.  
  610. set_up_initial_allocations()
  611.  
  612.     {
  613.     current_mns = INITIAL_MNS;
  614.     firstst = allocate_integer_array( current_mns );
  615.     lastst = allocate_integer_array( current_mns );
  616.     finalst = allocate_integer_array( current_mns );
  617.     transchar = allocate_integer_array( current_mns );
  618.     trans1 = allocate_integer_array( current_mns );
  619.     trans2 = allocate_integer_array( current_mns );
  620.     accptnum = allocate_integer_array( current_mns );
  621.     assoc_rule = allocate_integer_array( current_mns );
  622.     state_type = allocate_integer_array( current_mns );
  623.  
  624.     current_max_rules = INITIAL_MAX_RULES;
  625.     rule_type = allocate_integer_array( current_max_rules );
  626.     rule_linenum = allocate_integer_array( current_max_rules );
  627.  
  628.     current_max_scs = INITIAL_MAX_SCS;
  629.     scset = allocate_integer_array( current_max_scs );
  630.     scbol = allocate_integer_array( current_max_scs );
  631.     scxclu = allocate_integer_array( current_max_scs );
  632.     sceof = allocate_integer_array( current_max_scs );
  633.     scname = allocate_char_ptr_array( current_max_scs );
  634.     actvsc = allocate_integer_array( current_max_scs );
  635.  
  636.     current_maxccls = INITIAL_MAX_CCLS;
  637.     cclmap = allocate_integer_array( current_maxccls );
  638.     ccllen = allocate_integer_array( current_maxccls );
  639.     cclng = allocate_integer_array( current_maxccls );
  640.  
  641.     current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
  642.     ccltbl = allocate_character_array( current_max_ccl_tbl_size );
  643.  
  644.     current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
  645.  
  646.     current_max_xpairs = INITIAL_MAX_XPAIRS;
  647.     nxt = allocate_integer_array( current_max_xpairs );
  648.     chk = allocate_integer_array( current_max_xpairs );
  649.  
  650.     current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
  651.     tnxt = allocate_integer_array( current_max_template_xpairs );
  652.  
  653.     current_max_dfas = INITIAL_MAX_DFAS;
  654.     base = allocate_integer_array( current_max_dfas );
  655.     def = allocate_integer_array( current_max_dfas );
  656.     dfasiz = allocate_integer_array( current_max_dfas );
  657.     accsiz = allocate_integer_array( current_max_dfas );
  658.     dhash = allocate_integer_array( current_max_dfas );
  659.     dss = allocate_int_ptr_array( current_max_dfas );
  660.     dfaacc = allocate_dfaacc_union( current_max_dfas );
  661.     }
  662.